package io.bsly.modules.app.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.base.Objects;
import io.bsly.common.utils.PageUtils;
import io.bsly.common.utils.R;
import io.bsly.modules.app.entity.localEntity.DyHotelFavoriteEntity;
import io.bsly.modules.app.service.localmaster.DyHotelFavoriteService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;



/**
 * 收藏的酒店
 *
 * @author fys
 * @email demo@gmail.com
 * @date 2021-09-30 15:37:44
 */
@Api(tags = "收藏酒店")
@RestController
@RequestMapping("app/dyhotelfavorite")
public class DyHotelFavoriteController {
    @Autowired
    private DyHotelFavoriteService dyHotelFavoriteService;

    /**
     * 列表
     */
    @ApiOperation(value = "收藏的酒店列表",notes = "收藏酒店")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "用户ID" , dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "page",value = "当前页数" , dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "limit",value = "每页条数" , dataType = "string", paramType = "query")
    })
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = dyHotelFavoriteService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @ApiOperation(value = "根据id查询")
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id){
		DyHotelFavoriteEntity dyHotelFavorite = dyHotelFavoriteService.getById(id);
        return R.ok().put("dyHotelFavorite", dyHotelFavorite);
    }

    /**
     * 保存
     */
    @ApiOperation(value = "保存")
    @PostMapping("/save")
    public R save(@RequestBody DyHotelFavoriteEntity dyHotelFavorite){
        DyHotelFavoriteEntity one = dyHotelFavoriteService.getOne(new LambdaQueryWrapper<DyHotelFavoriteEntity>()
                .eq(DyHotelFavoriteEntity::getUserId, dyHotelFavorite.getUserId())
                .eq(DyHotelFavoriteEntity::getHotelId, dyHotelFavorite.getHotelId()));
        if (ObjectUtil.isNull(one)){
            dyHotelFavoriteService.save(dyHotelFavorite);
        }
        return R.ok();
    }

    /**
     * 修改
     */
    @ApiOperation(value = "根据ID更新")
    @PostMapping("/update")
    public R update(@RequestBody DyHotelFavoriteEntity dyHotelFavorite){
		dyHotelFavoriteService.updateById(dyHotelFavorite);
        return R.ok();
    }

    /**
     * 删除
     */
    @ApiOperation(value = "根据id数组删除")
    @DeleteMapping("/delete")
    public R delete(@RequestBody Integer[] ids){
		dyHotelFavoriteService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

}
